package com.ruoyi.system.service.construction.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.construction.ConstructionInfo;
import com.ruoyi.system.domain.construction.OrderTeam;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.construction.ConstructionInfoMapper;
import com.ruoyi.system.mapper.construction.OrderTeamMapper;
import com.ruoyi.system.mapper.zhuangxiumapper.shopMapper.SupplierInfoMapper;
import com.ruoyi.system.service.construction.OrderTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderTeamServiceImpl implements OrderTeamService {

    @Autowired
    private OrderTeamMapper orderTeamMapper;

    @Autowired
    private ConstructionInfoMapper constructionInfoMapper;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    /**
     * 查询施工队订单列表
     * @param orderTeam
     * @return
     */
    @Override
    public List<OrderTeam> selectOrderTeamList(OrderTeam orderTeam,Long userId){
        SupplierInfo supplierInfo = new SupplierInfo();
        supplierInfo.setCustomerId(userId);
        supplierInfo.setSupplierTypeform(2);
        // 创建集合储存供应商ID
        List<Long> supplierIds = new ArrayList<Long>();
        // 创建集合储存施工队人员ID
        List<Long> teamInfoIds = new ArrayList<Long>();
        // 根据用户ID查询供应商信息
        List<SupplierInfo> supplierInfoList = supplierInfoMapper.selectSupplierInfoByUserId(supplierInfo);
        if(supplierInfoList.size()>0){
            for (SupplierInfo supplier : supplierInfoList) {
                supplierIds.add(Long.valueOf(supplier.getSupplierId()));
            }
        }else{
            supplierIds.add(Long.valueOf(-1));
        }
        // 创建人员实体类
        ConstructionInfo constructionInfo = new ConstructionInfo();
        constructionInfo.setSupplierIds(supplierIds);
        // 根据供应商ID查询所有人员信息
        List<ConstructionInfo> constructionInfoList = this.constructionInfoMapper.selectConstructionInfo(constructionInfo);
        if(constructionInfoList.size()>0){
            // 添加人员ID到集合中
            for (ConstructionInfo cInfo : constructionInfoList) {
                teamInfoIds.add(cInfo.getConstructionId());
            }
        }else{
            teamInfoIds.add(Long.valueOf(-1));
        }
        orderTeam.setTeamInfoIds(teamInfoIds);
        return orderTeamMapper.selectOrderTeamList(orderTeam);
    }

    /**
     * 根据ID查询施工队订单信息
     * @param orderTeamId
     * @return
     */
    @Override
    public OrderTeam selectOrderTeamById(Long orderTeamId){
        return orderTeamMapper.selectOrderTeamById(orderTeamId);
    }

    /**
     * 添加施工队订单信息
     * @param orderTeam
     * @return
     */
    @Override
    public int insertOrderTeam(OrderTeam orderTeam){
        return orderTeamMapper.insertOrderTeam(orderTeam);
    }

    /**
     * 修改施工队订单信息
     * @param orderTeam
     * @return
     */
    public int updateOrderTeam(OrderTeam orderTeam){
        return orderTeamMapper.updateOrderTeam(orderTeam);
    }

    /**
     * 取消订单
     * @param orderTeamId
     * @return
     */
    @Override
    public int cancelOrderTeam(Long orderTeamId){
        return orderTeamMapper.cancelOrderTeam(orderTeamId);
    }

    /**
     * 确认完结订单
     * @param orderTeamId
     * @return
     */
    @Override
    public int finishOrderTeam(Long orderTeamId){
        return orderTeamMapper.finishOrderTeam(orderTeamId);
    }

    /**
     * 删除施工队订单信息
     * @param orderTeamId
     * @return
     */
    @Override
    public int deleteOrderTeamById(Long orderTeamId){
        return orderTeamMapper.deleteOrderTeamById(orderTeamId);
    }

    /**
     * 批量删除施工队订单信息
     * @param ids
     * @return
     */
    @Override
    public int deleteOrderTeamByIds(Long[] ids){
        return orderTeamMapper.deleteOrderTeamByIds(ids);
    }

    /**
     * 查询店铺列表
     * @param userId
     * @return
     */
    public List<SupplierInfo> selectSupplierList(Long userId){
        SupplierInfo supplierInfo = new SupplierInfo();
        supplierInfo.setCustomerId(userId);
        // 创建集合储存供应商ID
        List<Long> supplierIds = new ArrayList<Long>();
        // 根据用户ID查询供应商信息
        List<SupplierInfo> supplierInfoList = supplierInfoMapper.findAllSupplierInfo(supplierInfo);
        for (SupplierInfo supplier : supplierInfoList) {
            supplierIds.add(Long.valueOf(supplier.getSupplierId()));
        }
        return supplierInfoList;
    }

    //TODO:超管查询施工队店铺订单信息
    public List<OrderTeam> selectSuperOrderTeamList(OrderTeam orderTeam){
        return orderTeamMapper.selectSuperOrderTeamList(orderTeam);
    }
}
